package com.lingying.caiwudailikehudaun.verificationUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    /**
     * 日期转换为字符串 格式自定义
     *
     * @param date
     * @param f
     * @return
     */
    public static String dateStr(Date date, String f) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat format = new SimpleDateFormat(f);
        String str = format.format(date);
        return str;
    }

    /**
     * 计算两个日期之间相差的秒数
     *
     * @param date1
     * @param date2
     * @return date1>date2时结果<0,  date1=date2时结果=0, date2>date1时结果>0
     */
    public static int secondBetweenByDateStr(Date date1, Date date2) {
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        try {
            Date d1 = sdf.parse(DateUtil.dateStr(date1, "yyyy-MM-dd HH:mm:ss"));
            Date d2 = sdf.parse(DateUtil.dateStr(date2, "yyyy-MM-dd HH:mm:ss"));
            cal.setTime(d1);
            long time1 = cal.getTimeInMillis();
            cal.setTime(d2);
            long time2 = cal.getTimeInMillis();
            return Integer.parseInt(String.valueOf(((time2 - time1) / 1000L)));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }


    //大部分敏感词汇在10个以内，直接返回缓存的字符串
    //public static String[] starArr={"*","**","***","****","*****","******","*******","********","*********","**********"};

    /**
     * 生成n个星号的字符串
     *
     * @param length
     * @return
     */
    public static String getStarChar(int length) {
        if (length <= 0) {
            return "";
        }
        ////大部分敏感词汇在10个以内，直接返回缓存的字符串
        //if (length <= 10) {
        //    return starArr[length - 1];
        //}

        //生成n个星号的字符串
        char[] arr = new char[length];
        for (int i = 0; i < length; i++) {
            arr[i] = '*';
        }
        return new String(arr);
    }
}
